package com.hc.controller;

import com.hc.service.IRoomService;
import com.hc.vo.GetFloorVO;
import com.hc.vo.GetRoomVO;
import com.hc.vo.GetUnitVO;
import com.hc.vo.StatusVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.DispatcherServlet;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/building")
public class BuildingController {
    IRoomService roomService;

    @Autowired
    public void setRoomService(IRoomService roomService) {
        this.roomService = roomService;
    }

    @GetMapping("/floor")
    @ApiOperation(value = "根据社区获得楼栋列表", response = GetFloorVO.class, produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "GET")
    public StatusVO getFloorByCommunity(@RequestParam(required = false) String communityId) {
        List<GetFloorVO> data = roomService.getFloorByCommunityId(communityId).stream().map(ent -> {
            GetFloorVO vo = new GetFloorVO();
            BeanUtils.copyProperties(ent, vo);
            return vo;
        }).collect(Collectors.toList());
        return new StatusVO(data);
    }


    @GetMapping("/unit")
    @ApiOperation(value = "根据楼栋获得所有单元", response = GetUnitVO.class, produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "GET")
    public StatusVO getUnitByFloorId(@RequestParam(required = false) String floorId) {
        List<GetUnitVO> data = roomService.getUnitByFloorId(floorId).stream().map(ent -> {
            GetUnitVO vo = new GetUnitVO();
            BeanUtils.copyProperties(ent, vo);
            return vo;
        }).collect(Collectors.toList());
        return new StatusVO(data);
    }

    @GetMapping("/room")
    @ApiOperation(value = "根据单元获得所有房间", response = GetRoomVO.class, produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "GET")
    public StatusVO getRoomByUnitId(@RequestParam(required = false) String unitId) {
        List<GetRoomVO> data = roomService.getRoomByUnitId(unitId).stream().map(ent -> {
            GetRoomVO vo = new GetRoomVO();
            BeanUtils.copyProperties(ent, vo);
            return vo;
        }).collect(Collectors.toList());
        return new StatusVO(data);
    }
}
